<template>
  <div id="detail">
    <detail-nav-bar class="detail-nav"
                    @titleClick='titleClick'
                    ref="nav" />
    <scroll class="content"
            ref='scroll'
            @scroll="contentScroll "
            :probe-type='3'>

      <detail-swiper :top-images='topImages'></detail-swiper>
      <detail-base-info :goods='goods' />
      <detail-shop-info :shop="shop" />
      <detail-goods-info :detailInfo='detailInfo'
                         @imageLoad="imageLoad" />
      <detail-param-info :paramInfo="paramInfo"
                         ref="params" />
      <detail-comment-info ref="comment"
                           :comment-info="commentInfo"></detail-comment-info>
      <goods-list :goods='recommends'
                  ref='recommends' />
    </scroll>
    <detail-bottom-bar @addToCart='addToCart' />
    <back-top @click.native="backClick"
              v-show="isShowBackTop"></back-top>
    <toast :message='message'
           :show='show'></toast>
  </div>
</template>

<script>

import { itemListenerMixin, backTopMixin } from 'common/mixin.js'

import { getDetail, Goods, Shop, GoodsParam, getRecommend } from 'network/detail'

import GoodsList from 'components/content/goods/GoodsList.vue'
import BackTop from 'components/content/backTop/BackTop.vue'

import Scroll from 'components/common/scroll/Scroll'

import DetailNavBar from './childComps/DetailNavBar.vue'
import DetailSwiper from './childComps/DetailSwiper.vue'
import DetailBaseInfo from './childComps/DetailBaseInfo.vue'
import DetailShopInfo from './childComps/DetailShopInfo.vue'
import DetailGoodsInfo from './childComps/DetailGoodsInfo.vue'
import DetailParamInfo from './childComps/DetailParamInfo.vue'
import DetailCommentInfo from './childComps/DetailCommentInfo';
import DetailBottomBar from './childComps/DetailBottomBar.vue'

import { mapActions, mapGetters } from 'vuex'
import Toast from 'components/common/toast/Toast.vue'
export default {
  name: 'Detail',
  components: {
    DetailNavBar,
    DetailSwiper,
    DetailBaseInfo,
    DetailShopInfo,
    Scroll,
    DetailGoodsInfo,
    DetailParamInfo,
    DetailCommentInfo,
    GoodsList,
    DetailBottomBar,
    BackTop,
    Toast,

  },
  mixins: [itemListenerMixin, backTopMixin],
  data () {
    return {
      iid: null,
      topImages: [],
      goods: {},
      shop: {},
      detailInfo: {},
      paramInfo: {},
      commentInfo: {},
      recommends: [],
      themeTopYs: [],
      getThemeTopY: null,
      currentIndex: 0,
      message: '',
      show: false,

    }
  },
  created () {
    this.iid = this.$route.params.iid

    getDetail(this.iid).then(res => {

      const data = res.result
      this.topImages = res.result.itemInfo.topImages
      this.goods = new Goods(data.itemInfo, data.columns, data.shopInfo.services)
      this.shop = new Shop(data.shopInfo)
      this.detailInfo = data.detailInfo
      this.paramInfo = new GoodsParam(data.itemParams.info, data.itemParams.rule)
      if (data.rate.list) {
        this.commentInfo = data.rate.list[0];
      }
    })
    getRecommend().then(res => {
      this.recommends = res.data.list
    })
  },
  mounted () {
  },
  updated () {
  },
  destroyed () {

    this.$bus.$off('itemImgLoad', this.itemImgListener)

  },
  methods: {
    ...mapActions(['addCart']),
    imageLoad () {
      this.$refs.scroll.refresh()
      this.themeTopYs = []
      this.themeTopYs.push(0)
      this.themeTopYs.push(this.$refs.params.$el.offsetTop)
      this.themeTopYs.push(this.$refs.comment.$el.offsetTop)
      this.themeTopYs.push(this.$refs.recommends.$el.offsetTop)
      this.themeTopYs.push(Number.MAX_VALUE)

    },
    titleClick (index) {
      this.$refs.scroll.scrollTo(0, -this.themeTopYs[index] + 44, 500)
    },
    contentScroll (position) {
      const positionY = -position.y + 44
      let length = this.themeTopYs.length
      for (let i = 0; i < length - 1; i++) {
        if (this.currentIndex !== i && (i < length - 1 && positionY >= this.themeTopYs[i] && positionY < this.themeTopYs[i + 1])) {
          // if (this.currentIndex !== i && ((i < length - 1 && positionY >= this.themeTopYs[i] && positionY < this.themeTopYs[i + 1]) || (i === length - 1 && positionY > this.themeTopYs[i]))) {
          this.currentIndex = i;
          this.$refs.nav.currentIndex = this.currentIndex
        }
      }
      this.isShowBackTop = (-position.y) > 1000

    },
    addToCart () {
      const product = {}
      product.image = this.topImages[0];
      product.title = this.goods.title;
      product.desc = this.goods.desc;
      product.price = this.goods.realPrice;
      product.iid = this.iid;

      this.addCart(product).then(res => {
        // this.message = res;
        // this.show = true;
        // setTimeout(() => {
        //   this.show = false;
        //   this.message = ''
        // }, 1500);
        this.$toast.show(res, 2000)
        // console.log(this.$toast);
        // this.$toast.show(res, 2000)
      })
      // this.$store.dispatch('addCart', product).then(res => {
      //   console.log(res);
      // })


    },

  },
}

</script>
<style  scoped>
#detail {
  position: relative;
  z-index: 9;
  background-color: #fff;
  height: 100vh;
}

.detail-nav {
  position: relative;
  z-index: 9;
  background-color: #fff;
}

.content {
  height: calc(100% - 44px - 58px);
  overflow: hidden;
}
</style>